package com.easycarebn.main.fragment;

import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.data.DoctorDetail;
import com.easycarebn.request.data.DoctorDetail.Address;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.squareup.picasso.Picasso;
import com.timbacsi.main.R;

public class FragmentMapDetailt extends Fragment {
    public static final String FRAGMENT_TAG = "RESULT_MAP";

    private View mView;
    private static FragmentMapDetailt mFragmentResultMap = null;
    private FragmentActivity mFragmentActivity;
    private GoogleMap mMap;
    private HashMap<Marker, Address> mMapItemDr;
    private IResult callIResult;

    private Marker marker = null;

    public FragmentMapDetailt() {
        mMapItemDr = new HashMap<Marker, Address>();
    }

    public static FragmentMapDetailt getInstance() {
        if (mFragmentResultMap == null) {
            mFragmentResultMap = new FragmentMapDetailt();
        }
        return mFragmentResultMap;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof FragmentActivity) {
            mFragmentActivity = (FragmentActivity) activity;
        }
        if (activity instanceof IResult) {
            callIResult = (IResult) activity;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_result_map, null);
        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        mMap = ((SupportMapFragment) mFragmentActivity.getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        if (FragmentDrDetailt.newInstance("").getListAddresses() == null)
            return;

        for (Address item : FragmentDrDetailt.newInstance("").getListAddresses()) {
            LatLng latlng = getlatLng(item.getMap_position());

            if (latlng != null) {
                Marker marker = mMap.addMarker(new MarkerOptions().position(latlng).icon(
                        BitmapDescriptorFactory.fromResource(R.drawable.ic_location_map)));
                mMapItemDr.put(marker, item);
            }
        }
        // Move the camera instantly to hamburg with a zoom of 15.
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(getlatLng(FragmentDrDetailt.newInstance("").getListAddresses().get(0)
                .getMap_position()), 15));

        // Zoom in, animating the camera.
        mMap.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
        mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                callIResult.onDisplay(Utils.ONBACK, "");
            }
        });

        mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

        super.onActivityCreated(savedInstanceState);
    }

    private class CustomInfoWindowAdapter implements InfoWindowAdapter {

        private View view;

        public CustomInfoWindowAdapter() {
            view = getActivity().getLayoutInflater().inflate(R.layout.layout_doctor_info_map, null);
        }

        @Override
        public View getInfoContents(Marker marker) {
            DoctorDetail doctorDetail = FragmentDrDetailt.newInstance("").getCurrentDoctor();

            FragmentMapDetailt.this.marker = marker;
            final Address info = mMapItemDr.get(marker);
            TextView tv_address = (TextView) view.findViewById(R.id.dr_dia_chi);
            tv_address.setText(info.getAddress());

            final ImageView imgAvatar = (ImageView) view.findViewById(R.id.dr_avatar);

            TextView tvName = (TextView) view.findViewById(R.id.dr_name);
            TextView tvChuyenKhoa = (TextView) view.findViewById(R.id.dr_chuyen_khoa);

            RatingBar mRatingBar = (RatingBar) view.findViewById(R.id.dr_star);

            Picasso.with(mFragmentActivity).load(doctorDetail.getAvatar()).placeholder(R.drawable.avata).error(R.drawable.avata)
                    .into(imgAvatar);

            tvName.setText(doctorDetail.getName());
            if (doctorDetail.getSpeciality() != null) {
                tvChuyenKhoa.setText(doctorDetail.getSpeciality().get(0));
            }
            mRatingBar.setRating(doctorDetail.getRating());

            return view;

        }

        @Override
        public View getInfoWindow(final Marker marker) {
            if (FragmentMapDetailt.this.marker != null && FragmentMapDetailt.this.marker.isInfoWindowShown()) {
                FragmentMapDetailt.this.marker.hideInfoWindow();
                FragmentMapDetailt.this.marker.showInfoWindow();
            }

            return null;

        }
    }

    public static LatLng getlatLng(String mapAdd) {
        System.out.println("LatLng: " + mapAdd);
        if (TextUtils.isEmpty(mapAdd)) {
            return null;
        }
        String[] latlng = mapAdd.split(",");
        if (latlng == null || latlng.length < 2) {
            return null;
        }
        float lat = Float.parseFloat(latlng[0]);
        float lng = Float.parseFloat(latlng[1]);
        return new LatLng(lat, lng);
    }
}
